package com.dyb.recuitment.controller;


import com.dyb.recuitment.common.Result;
import com.dyb.recuitment.entity.JobLike;
import com.dyb.recuitment.entity.Shield;
import com.dyb.recuitment.service.IJobLikeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 职位收藏表 前端控制器
 * </p>
 *
 * @author 邓宇斌
 * @since 2022-02-22
 */
@RestController
@Api(tags = "职位收藏表(JobLike)")
@RequestMapping("/job_like")
public class JobLikeController {

    @Autowired
    private IJobLikeService jobLikeService;

    @ApiOperation(value = "收藏职位", notes = "通过用户ID、职位ID收藏职位 " +
            "{\"userId\":1,\"jobId\":1}")
    @ApiImplicitParam(name = "jobLike", value = "JobLike类")
    @PostMapping("/add")
    public Object add(@RequestBody JobLike jobLike) {
        Object data = null;
        try {
            data = jobLikeService.add(jobLike);
//            userLogService.add(new UserLog(shield.getUserId(), "添加收藏职位"));
            if (data == null || (int) data == 0) {
                return Result.warning(data, "收藏失败!");
            }
            return Result.success(data, "收藏成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "取消收藏", notes = "通过ID取消收藏职位 " + "{\"id\":1}")
    @ApiImplicitParam(name = "jobLike", value = "JobLike类")
    @PostMapping("/delete")
    public Object delete(@RequestBody JobLike jobLike) {
        Object data = null;
        try {
            data = jobLikeService.delete(jobLike);
//            userLogService.add(new UserLog(shield.getUserId(), "取消收藏"));
            if (data == null || data == (Integer) 0) {
                return Result.failure(data, "取消收藏失败!");
            }
            return Result.success(data, "取消收藏成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "查询收藏职位", notes = "按条件查询收藏职位 {\"userId\":1}")
    @ApiImplicitParam(name = "jobLike", value = "JobLike类")
    @PostMapping("/selectByJobLike")
    public Object selectByJobLike(@RequestBody JobLike jobLike) {
        Object data = null;
        try {
            data = jobLikeService.selectByJobLike(jobLike);
//            userLogService.add(new UserLog(shield.getUserId(), "查询收藏职位"));
            if (data == null || ((List) data).size() <= 0) {
                return Result.warning(data, "还未收藏职位!");
            }
            return Result.success(data, "查询成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "查询所有收藏职位", notes = "查询所有收藏职位")
    @GetMapping("/selectAllJobLike")
    public Object selectAllJobLike() {
        Object data = null;
        try {
            data = jobLikeService.selectByAllLike();
//            userLogService.add(new UserLog(shield.getUserId(), "查询所有屏蔽项"));
            if (data == null || ((List) data).size() <= 0) {
                return Result.warning(data, "收藏职位表为空");
            }
            return Result.success(data, "查询成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

}
